package com.tubang.organization.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tubang.common.util.ResultData;
import com.tubang.organization.entity.MenuEntity;
import com.tubang.organization.entity.RoleEntity;
import com.tubang.organization.entity.RoleMenuEntity;
import com.tubang.organization.service.IMenuService;
import com.tubang.organization.service.IRoleMenuService;
import com.tubang.organization.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/config")
public class ConfingController {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    private IRoleService roleService;
    @Resource
    private IRoleMenuService roleMenuService;
    @Resource
    private IMenuService menuService;

    /**
     * 生成权限角色关系表
     *
     * @return
     */
    @PostMapping(value = "creatRoleMenuList")
    public ResultData creatRoleMenuList() {
        //生成一下关系表
        //查询角色表 拿到所有角色
        EntityWrapper<RoleEntity> roleMapper = new EntityWrapper<RoleEntity>();
        List<RoleEntity> roleEntities = roleService.selectList(roleMapper);
        for (RoleEntity roleEntity : roleEntities) {
            //遍历角色表  查询角色菜单
            String roleId = roleEntity.getId();
            List<MenuEntity> menuEntities = menuService.selectListByParentId(roleId);
            for (MenuEntity menuEntity : menuEntities) {
                RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
                roleMenuEntity.setRoleId(roleId);
                roleMenuEntity.setMenuId(menuEntity.getId());
                roleMenuService.insert(roleMenuEntity);
            }
        }

        return ResultData.result(true);
    }
}
